package cn.lena.idle.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import cn.lena.idle.pojo.Collection;
import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.Goods;
import cn.lena.idle.util.CurrencySQL;
import cn.lena.idle.util.DateUtils;

/**
 * @author lena
 * @date 2021/6/13
 */
public class CollectionDbHelper extends SQLiteOpenHelper {

    // 定义数据库表名
    public static final String DB_NAME = "tb_collection";
    // 创建用户信息表
    private static final String CREATE_COLLECTION_DB = "create table "+DB_NAME+" (" +
            "id integer primary key autoincrement," +
            "goodsId integer," +
            "userId integer," +
            "addDate varchar(50))";

    /**
     * 用户收藏商品
     * @param c ：记录
     * @return 主键id
     */
    public int add(Collection c){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("userId", c.getUserId());
        values.put("goodsId",c.getGoodsId());
        values.put("addDate", c.getAddDate());
        int result=(int)db.insert(DB_NAME,null,values);
        return result;
    }

    /**
     * 取消收藏
     * @param id 收藏id
     * @return
     */
    public int delById(Integer id){
        SQLiteDatabase db=this.getWritableDatabase();
        int result = 0;
        if (db.isOpen()){
            result = db.delete(DB_NAME, "id = ?", new String[]{id.toString()});
        }
        return result;
    }

    /**
     * 取消收藏
     * @param userId : 用户id
     * @param goodsId : 商品id
     * @return
     */
    public int delByUserAndGoods(Integer userId,Integer goodsId){
        SQLiteDatabase db=this.getWritableDatabase();
        int result = 0;
        if (db.isOpen()){
            result = db.delete(DB_NAME, "userId = ? and goodsId = ? ", new String[]{userId.toString(),goodsId.toString()});
        }
        return result;
    }


    /**
     * 获取用户收藏的商品列表
     * @param id : 用户id
     * @return
     */
    public List<Integer> getGoodsId(Integer id){
        List<Integer> list=new ArrayList<>();
        SQLiteDatabase db=this.getWritableDatabase();
        String sql="select goodsId from tb_collection where userId = ? ";
        Cursor cursor=db.rawQuery(sql.toString(),new String[]{id.toString()});
        while (cursor.moveToNext()){
            list.add(cursor.getInt(0));
        }
        return list;
    }


    public CollectionDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_COLLECTION_DB);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 销毁
        db.execSQL(CurrencySQL.DROP_SQL + DB_NAME);
        // 重建
        db.execSQL(CREATE_COLLECTION_DB);
    }

}
